package com.ws.lv.config;

import com.ws.lv.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan({"com.ws.lv.controller","com.ws.lv.util","com.ws.lv.interceptor"})//扫描controller和util
@PropertySource("classpath:file.properties")//获取配置文件
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    /**
     * 创建视图解析器
     */
    @Bean(name = "viewResolver")
    public ViewResolver viewResolverInit(){
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/");//设置前缀
        viewResolver.setSuffix(".jsp");//设置后缀
        return viewResolver;
    }
    /**
     * 启用默认静态资源处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //启用静态处理
        configurer.enable();
    }

    //文件传输配置  方法名是multipartResolver 或者bean中配置名字multipartResolver
    @Bean("multipartResolver")
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000000);
        //设置上传文件大小最大值
        return multipartResolver;
    }


     //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/adminpage/*","/acl_user/selectAcl_userByLogin","/acl_user/toLogin","/acl_user/outLogin","/homepage/*","/user/homeLogin","/user/outLogin","/user/qiehuan","/user/zhuxiao","/user/userRegister");
    }

}